<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2018/11/30
 * Time: 9:32
 */

namespace app\admin\controller;


use app\admin\model\Account;
use app\admin\service\PopularizeService;
use app\admin\validate\LayuiPageValidate;
use app\admin\validate\RepeatValidate;
use app\admin\validate\StatusValidate;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\MysqlErrorException;
use Page\Page;
use think\facade\Request;
use app\admin\model\Popularize as PopularizeModel;
use app\admin\model\Registered as RegisteredModel;

class Popularize extends BaseController
{
    # 渲染全部推广页面
    public function getAllPopularize()
    {
        $model = new Account();
        $name = $model->field('name')->select();
        $this->assign('name',$name);
        return $this->fetch('popularize/allPopularize');
    }

    public function index($id = null)
    {
        if($id){
            $result = Account::findOne($id);
            $this->assign('info',$result);
        }else{
            $arr = [
                'status' => '',
                'repeat' => '',
                'message_id' => '',
            ];
            $this->assign('info', $arr);
        }
        return $this->fetch('popularize/addPopularize');
    }
    # 添加或者更新  推广账号密码
    public function addAccount()
    {
        $data = Request::post();
        # 先判断客户端传过来的参数有没有id
        if($data['id']){
            # 存在则更新
            $result = PopularizeService::upAccount($data);
        }else {
            # 不存在则新建
            $result = PopularizeService::addAccount($data);
        }
        if (!$result['success']){
            return $this->error($result['msg'],'/admin/Popularize/index');
        }
        return $this->success($result['msg'],'/admin/Popularize/Management');
    }
    # 删除管理账号
    public function delAccount()
    {
        $id = Request::post('id');
        $resulr = Account::delOne($id);
        if (!$resulr){
            throw new MysqlErrorException([
               'code' => 300,
               'msg' => '删除失败'
            ]);
        }
        return json([
           'code' => 200
        ]);
    }
    # 管理推广账号
    public function Management()
    {
        # 接口url设置为动态的
        return $this->fetch('popularize/Management');
    }
    # 获取所有推广账号api 并在layui的table模块进行分页
    public function allAccount($page,$limit)
    {
        $allAccount  = PopularizeService::getAll($page,$limit);
        if(!$allAccount){
        }
        return json([
           'code' => 0,
           'data' => $allAccount[0],
            'count' => $allAccount[1]
        ]);
    }
    # 修改状态(废弃)
    public function upStatus()
    {
        (new StatusValidate()) -> goCheck();
        $pose_data = Request::post();
        $result = Account::upStatus($pose_data);
        if(!$result){
            return json([
                'code' => 400,
                'msg' => 'mysql错误'
            ]);
        }
        return json([
           'code' => 400,
            'data' => $pose_data['status'] == '1' ? 0 : 1
        ]);

    }

    # 修改状态(废弃)
    public function upRepeat()
    {
        (new RepeatValidate()) ->goCheck();
        $pose_data = Request::post();
        $result = Account::upRepeat($pose_data);
        if(!$result){
            return json([
                'code' => 400,
                'msg' => 'mysql错误'
            ]);
        }
        return json([
            'code' => 200,
            'data' => $pose_data['repeat'] == '1' ? 0 : 1
        ]);

    }

    # 按条件查找出对应的推广账号
    public function findAccount()
    {
        $data = Request::get();
        $result = PopularizeService::findAccount($data);
        return json([
           'code' => 0,
           'count' =>  $result[0],
            'data' =>  $result[1]
        ]);
    }
    # 获取全部注册的接口
    public function getAllPopularizeApi($model_str)
    {
        (new LayuiPageValidate())->goCheck();
        $get_data = Request::get();
        $classModel = 'app\\admin\\model\\'.$model_str;
        $model = new $classModel;
        $page = new Page($model,$get_data['page'],$get_data['limit'],[],['add_time'=>'desc']);
        $data = $page->layuiPage();
        return json($data);
    }
    # 按条件查询接口
    public function findAllPopularizeApi($model_str)
    {
        (new LayuiPageValidate())->goCheck();
        $post_data = Request::param();
        $service = new PopularizeService();
        $result = $service->findAllPopularizeWhere($post_data,$model_str);
        return json($result);
    }
    # 查找推广方所有标识码
    public function findSign()
    {
        $company = Request::post('name');
        $service = new PopularizeService();
        $data = $service->findAllSign($company);
        return json([
            'code' => 200,
            'data' => $data
        ]);
    }

    # 删除推广记录
    public function delOnePopularizeApi($id)
    {
        (new IDMustBePositiveInt()) -> goCheck();
        $result = PopularizeModel::destroy($id);
        if(!$result){
            throw new MysqlErrorException([
                'msg' => '服务器出错',
                'code' => 500
            ]);
        }
        return json([
            'code' => 200,
            'msg' => 'success'
        ]);
    }

    # 隐藏推广记录，把status状态改成0
    public function hideOneRegistered($id)
    {
        (new IDMustBePositiveInt()) -> goCheck();
        $model = new RegisteredModel;
        $result = $model->hideOne($id);
        return json([
            'code' => 200,
            'msg' => 'success',
            'status_code' => $result
        ]);
    }
    # 根据id删除对应的记录
    public function delCheckListApi()
    {
        $ids = Request::post('ids');
        $ids_arr = json_decode($ids, true);
        $model = new PopularizeModel();
        $model->delCheck($ids_arr);
        return json(['code' => 200]);
    }
}